package com.example.walkingtour.maps;

import java.util.List;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.example.walkingtour.data.WTDatabase;
import com.example.walkingtour.data.WTProvider;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/*
 * Helper class for TourMapActivity
 */
public class TourMapActivityHelper {

	private TourMapItemizedOverlay mOverlay;

	private final static String[] COORDINATE_COLUMNS = new String[] {
			WTDatabase.KEY_ROWID, WTDatabase.COL_PATH, WTDatabase.COL_FILE,
			WTDatabase.COL_GPS_LAT, WTDatabase.COL_GPS_LONG };

	public TourMapActivityHelper() {
	}

	public TourMapActivityHelper(TourMapItemizedOverlay mOverlay) {
		this();
		this.mOverlay = mOverlay;
	}

	public TourMapActivityHelper(List<Overlay> mMapOverlays,
			TourMapItemizedOverlay mOverlay) {
		this.mOverlay = mOverlay;
	}

	/*
	 * Adds the overlays to the map.
	 * 
	 * @param mFirstItem the first element in the database
	 * 
	 * @param mContentResolver the content resolver from TourMapActivity
	 * 
	 * @return a TourMapItemized overlay object containing the overlays
	 */
	public TourMapItemizedOverlay addOverlaysToMap(int mFirstItem,
			ContentResolver mContentResolver) {
		try {
			doAddOverlayToMap(mFirstItem, mContentResolver);
		} catch (Exception ex) {
			Log.e("Creating map overlays", ex.getMessage());
		}
		return this.mOverlay;
	}

	private void doAddOverlayToMap(int mFirstItem,
			ContentResolver mContentResolver) {
		Cursor mCursor;
		Uri item = Uri.withAppendedPath(WTProvider.CONTENT_URI,
				String.valueOf(mFirstItem));

		// database is queried for all rows.
		mCursor = mContentResolver.query(item, COORDINATE_COLUMNS, null, null,
				null);

		try {
			if (mCursor != null)
				while (mCursor.moveToNext())
					addOverlay(mCursor);
		} catch (Exception e) {
			Log.d("TourMapActivity", e.getMessage());
		} finally {
			mCursor.close();
		}
	}

	/*
	 * Adds an mOverlay item (i.e. the android marker) to the map
	 * 
	 * @param cursor an object connected to the database where the media (i.e.
	 * video) file path and latitude/longitude coordinates are retrieved.
	 */
	private void addOverlay(Cursor mCursor) {
		String mFullFilePath = getMediaFullFilePath(mCursor);
		String mLongitude = mCursor.getString(mCursor
				.getColumnIndex(WTDatabase.COL_GPS_LONG));
		String mLatitude = mCursor.getString(mCursor
				.getColumnIndex(WTDatabase.COL_GPS_LAT));
		String latitudeAndLongitude = "Latitude: " + mLatitude + "\n"
				+ "Longitude: " + mLongitude;

		GeoPoint mPoint = new LatLonPoint(Double.parseDouble(mLatitude),
				Double.parseDouble(mLongitude));
		OverlayItem overlayItem = new OverlayItem(mPoint, mFullFilePath,
				latitudeAndLongitude);

		mOverlay.addOverlay(overlayItem);
	}

	private String getMediaFullFilePath(Cursor cursor) {
		String mPath = cursor.getString(cursor
				.getColumnIndex(WTDatabase.COL_PATH));
		String mFile = cursor.getString(cursor
				.getColumnIndex(WTDatabase.COL_FILE));
		return mPath + "/" + mFile;
	}
}

/*
 * Convert the latitude/longitude coordinates to GPS coordinates. For example,
 * coordinate pair 1,1 is converted to 1E6, 1E6
 */
final class LatLonPoint extends GeoPoint {
	public LatLonPoint(double mLatitude, double mLongitude) {
		super((int) (mLatitude * 1E6), (int) (mLongitude * 1E6));
	}
}